﻿package com.hotel.front;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.SharedCache;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.hotel.BL.ReasonCancelLogic;
import com.hotel.sampleTool.BlockSample;
import com.hotel.sampleTool.BookingPolicySample;
import com.hotel.sampleTool.CancellationPolicySample;
import com.hotel.sampleTool.CleaningStatusSample;
import com.hotel.sampleTool.CounterSample;
import com.hotel.sampleTool.DepartmentSample;
import com.hotel.sampleTool.DisplayColorSample;
import com.hotel.sampleTool.GeneralSample;
import com.hotel.sampleTool.HotelCustomSettingSample;
import com.hotel.sampleTool.HotelSample;
import com.hotel.sampleTool.HouseKeepingUserSample;
import com.hotel.sampleTool.MasterCreatorSample;
import com.hotel.sampleTool.MessageCheckInSample;
import com.hotel.sampleTool.MessageCheckOutSample;
import com.hotel.sampleTool.MessageFolioSample;
import com.hotel.sampleTool.ModeArrivalSimple;
import com.hotel.sampleTool.CreditCardTypeSample;
import com.hotel.sampleTool.POSTableSample;
import com.hotel.sampleTool.POSZoneSample;
import com.hotel.sampleTool.POSCategorySample;
import com.hotel.sampleTool.POSItemSample;
import com.hotel.sampleTool.POSKitchenSample;
import com.hotel.sampleTool.POSSample;
import com.hotel.sampleTool.POSTaxSample;
import com.hotel.sampleTool.POSUnitSample;
import com.hotel.sampleTool.PackagePriceSample;
import com.hotel.sampleTool.PackageSample;
import com.hotel.sampleTool.PayTypeSample;
import com.hotel.sampleTool.ReservationStatusSample;
import com.hotel.sampleTool.RoomSample;
import com.hotel.sampleTool.RoomTypeSample;
import com.hotel.sampleTool.SalutationSample;
import com.hotel.sampleTool.SourcesBusinessSample;
import com.hotel.sampleTool.TaskSample;
import com.hotel.sampleTool.TaxSample;
import com.hotel.sampleTool.UserLevelSample;
import com.hotel.sampleTool.UserSample;


@SuppressWarnings("serial")
public class RequestFactory extends HttpServlet {
	
	protected static DatastoreService datastore = DatastoreServiceFactory
			.getDatastoreService();
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, IllegalArgumentException {
		resp.setContentType("text/plain; charset=utf-8");
		resp.setHeader("Cache-Control", "no-cache");

		SharedCache.ClearAllCache();
		//Create Master
		CreateMasterData(req, resp);

		//Create Sample Data
		try {
			CreateSampleData(req, resp);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void CreateMasterData(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		MasterCreatorSample master = new MasterCreatorSample();
		master.CreateLanguageMaster();
		resp.getWriter().println("LanguageMaster Created");

		master.CreateCurrencyMaster();
		resp.getWriter().println("CurrencyMaster Created");
		
		master.CreateDateTimeFormatMaster();
		resp.getWriter().println("DateTimeFormatMaster Created");
		
		master.CreatePayModeMaster();
		resp.getWriter().println("PayModeMaster Created");
	}
	
	private void CreateSampleData(HttpServletRequest req, HttpServletResponse resp) throws IOException, EntityNotFoundException
	{
		//Create Hotel
		HotelSample hotel = new HotelSample();
		List<Entity> hotelEntity = hotel.SearchHotelId(1);
		if(hotelEntity == null || hotelEntity.size() == 0)
		{
			hotel.CreateSample();
			resp.getWriter().println("Hotel Created");
		}
		else resp.getWriter().println("Hotel Exist");
		
		//Create CleaningStatus
		CleaningStatusSample cleaningStatus = new CleaningStatusSample(req,resp);
		cleaningStatus.CreateSample();
		resp.getWriter().println("CleaningStatus Created");
		
		//Create ReservationStatus
		ReservationStatusSample reservationStatus = new ReservationStatusSample(req,resp);
		reservationStatus.CreateSample();
		resp.getWriter().println("ReservationStatus Created");
		
		//Create ReservationStatus
		DepartmentSample departmentSample = new DepartmentSample();
		departmentSample.CreateSample();
		resp.getWriter().println("Department Created");
		
		//Create Tax
		TaxSample ts = new TaxSample(req,resp);
		if(! ts.IsExist(EntityName.Tax))
		{
		ts.CreateSample();
		resp.getWriter().println("Tax Created");
		}
		else resp.getWriter().println("Tax Exist");

		//Create RoomType
		RoomTypeSample roomType = new RoomTypeSample(req,resp);
		roomType.CreateSample();
		resp.getWriter().println("RoomType Created");
		
		//Create Block
		BlockSample block = new BlockSample(req,resp);
		block.CreateSample();
		resp.getWriter().println("Block Created");
		
		//Create Room
		RoomSample room = new RoomSample(req,resp);
		room.CreateSample();
		resp.getWriter().println("Room Created");
		
		//Create General
		GeneralSample general = new GeneralSample(req,resp);
		general.CreateSample();
		resp.getWriter().println("General Created");
		
		//Create Task for hotel1
		TaskSample task = new TaskSample();
		if(!task.IsExist(EntityName.Task))
		{
			task.CreateSample(5);
			resp.getWriter().println("Task Created");
		}
		else resp.getWriter().println("Task Exist");

		//Create Salutation
		SalutationSample salutation = new SalutationSample();
		salutation.CreateSample();
		resp.getWriter().println("Salutation Created");
		
		//Create Counter
		CounterSample counter = new CounterSample();
		counter.CreateSample();
		resp.getWriter().println("Counter Created");
		
		//Create CancellationPolicy
		CancellationPolicySample cps = new CancellationPolicySample();
		if(!cps.IsExist(EntityName.CancellationPolicy))
		{	
			cps.CreateSample();
			resp.getWriter().println("CancellationPolicy Created");
		}
		else resp.getWriter().println("CancellationPolicy Exist");
		
		//Create BookingPolicy
		BookingPolicySample bps = new BookingPolicySample();
		if(!bps.IsExist(EntityName.BookingPolicy))
		{
			bps.CreateSample();
			resp.getWriter().println("BookingPolicy Created");
		}
		else resp.getWriter().println("BookingPolicy Exist");
				
		//Create Guest
		//GuestSample gs = new GuestSample();
		//if(!gs.IsExist(EntityName.Guest))
		//{
		//	gs.CreateSample();
		//	resp.getWriter().println("Guest Created");
		//}
		//else resp.getWriter().println("Guest Exist");

		//Create GuestMapping
//		GuestMappingSample gms = new GuestMappingSample();
//		if(!gms.IsExist(EntityName.GuestMapping))
//		{
//			gms.CreateSample();
//			resp.getWriter().println("GuestMapping Created");
//		}
//		else resp.getWriter().println("GuestMapping Exist");

		//Create PackagePrice
		PackagePriceSample pps = new PackagePriceSample(req,resp);
		pps.CreateSample();
		resp.getWriter().println("PackagePrice Created");
				
		//Create Package
		PackageSample ps = new PackageSample(req,resp);
		ps.CreateSample();
		resp.getWriter().println("Package Created");
				  
		//AddguestSample
		ReasonCancelLogic reasonCancelLogic = new ReasonCancelLogic();
		reasonCancelLogic.AddReasonCancelSample();
				  
		//SourcesBusiness Sample
		SourcesBusinessSample aa = new SourcesBusinessSample();
		if(! aa.IsExist(EntityName.SourcesBusiness))
		{
			aa.CreateSample();
			resp.getWriter().println("SourcesBusiness Created");
		}
		else resp.getWriter().println("SourcesBusiness Exist");
				
		//Create display color
		DisplayColorSample dc = new DisplayColorSample();
		if(! dc.IsExist(EntityName.AlphabetCoding))
		{
			dc.CreateSample();
			resp.getWriter().println("AlphabetCoding Created");
		}
		else resp.getWriter().println("AlphabetCoding Exist");
					
		//Create paytype
		PayTypeSample paytype = new PayTypeSample();
		if(! paytype.IsExist(EntityName.PayType)){
			paytype.CreateSample();
			resp.getWriter().println("PayType Created");
		}else{
			resp.getWriter().println("PayType Exist");
		}

		//Create POSTax
		POSTaxSample postax = new POSTaxSample(req,resp);
		postax.CreateSample();
		resp.getWriter().println("POSTax Created");
		
		//Create creditCardtype
		CreditCardTypeSample ccType = new CreditCardTypeSample();
			if(! ccType.IsExist(EntityName.CreditCardType)){
				ccType.CreateSample();
				resp.getWriter().println("CreditCardType Created");
			}else{
				resp.getWriter().println("CreditCardType Exist");
			}

		//Create ModeArrival
		ModeArrivalSimple modearrival = new ModeArrivalSimple();
		modearrival.CreateSample();
		resp.getWriter().println("ModeArrival Created");
		
		
		POSSample pos = new POSSample(req,resp);
		pos.CreateSample();
		resp.getWriter().println("POS Created");
		
		POSZoneSample z = new POSZoneSample(req,resp);
		z.CreateSample();
		resp.getWriter().println("POSZone Created");
	
		if(! modearrival.IsExist(EntityName.ModeArrival)){
			modearrival.CreateSample();
			resp.getWriter().println("ModeArrival Created");
		}else{
			resp.getWriter().println("ModeArrival Exist");
		}
		
		//Create POSUnit
		POSUnitSample posunit = new POSUnitSample(req,resp);
		posunit.CreateSample();
		resp.getWriter().println("POSUnit Created");

		//Create POSUnit
		POSKitchenSample poskitchen = new POSKitchenSample(req,resp);
		poskitchen.CreateSample();
		resp.getWriter().println("POSKitchen Created");

		//Create POSCategory
		POSCategorySample posCategory = new POSCategorySample(req,resp);
		posCategory.CreateSample();
		resp.getWriter().println("POSCategory Created");

		//Create POSItem
		POSItemSample posItem = new POSItemSample(req,resp);
		posItem.CreateSample();
		resp.getWriter().println("POSItem Created");
		
		POSTableSample tb = new POSTableSample(req,resp);
		tb.CreateSample();
		resp.getWriter().println("POSTable Created");

		//Create Counter
		UserLevelSample userlv = new UserLevelSample(req,resp);
			userlv.CreateSample();
			resp.getWriter().println("UserLevel Created");
		//Create User
		UserSample usr = new UserSample(req,resp);
		usr.CreateSample();
		resp.getWriter().println("User Created");
		
		//Create housekeepingUser
		HouseKeepingUserSample housekeepingUser = new HouseKeepingUserSample(req,resp);
		housekeepingUser.CreateSample();
		resp.getWriter().println("HouseKeepingUser Created");
		
		//Create Messagefolio
		MessageFolioSample messagefolio = new MessageFolioSample(req,resp);
		if(! messagefolio.IsExist(EntityName.MessageFolio)){
			messagefolio.CreateSample();
			resp.getWriter().println("MessageFolio Created");
		}else{
			resp.getWriter().println("MessageFolio Exist");
		}
		
		//Create MessageLogin
		MessageCheckInSample messageLogin = new MessageCheckInSample(req,resp);
		if(! messageLogin.IsExist(EntityName.MessageCheckIn)){
			messageLogin.CreateSample();
			resp.getWriter().println("MessageCheckin Created");
		}else{
			resp.getWriter().println("MessageCheckin Exist");
		}
		
		//Create MessageLogout
		MessageCheckOutSample messageLogout = new MessageCheckOutSample(req,resp);
		if(! messageLogout.IsExist(EntityName.MessageCheckOut)){
			messageLogout.CreateSample();
			resp.getWriter().println("MessageCheckout Created");
		}else{
			resp.getWriter().println("MessageCheckout Exist");
		}
		
		//Create HotelCustomSetting
		HotelCustomSettingSample hotelcustomsetting = new HotelCustomSettingSample(req,resp);
		if(! hotelcustomsetting.IsExist(EntityName.HotelCustomSetting)){
			hotelcustomsetting.CreateSample();
			resp.getWriter().println("HotelCustomSetting Created");
		}else{
			resp.getWriter().println("HotelCustomSetting Exist");
		}
	}

}
